package cc.eddic.practice.spring.domain;

import cc.eddic.practice.spring.jpa.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "t_category")
@Builder
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor


public class Category extends BaseEntity {
    public Category(String name){
        this.name = name;
        subCategories = new ArrayList<>();
    }
    public Category add (SubCategory subCategory){
        subCategory.setCategory(this);
        subCategories.add(subCategory);
        return this;
    }


    @Column(length = 50)
    private String name;

    @JsonIgnore
    @ToString.Exclude
    @OneToMany(mappedBy = "category",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    private List<SubCategory> subCategories;


}
